package fr.grobim.hangovers.gestionBudget.actions.support;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import fr.grobim.hangovers.gestionBudget.actions.utils.BeanComparator;

public abstract class AbstractActionSupportAvecTableau<T> extends AbstractActionSupport {

	private static final long serialVersionUID = 1L;
	
	private Integer rows = 0;
	private Integer page = 0;
	private String sord;
	private String sidx;
	private String searchField;
	private String searchString;
	private String searchOper;
	private Integer total = 0;
	private Integer records = 0;
	
	private List<T> listeTableau;
	
	public void raffraichirTableau() {
		records = this.listeTableau.size();
		total = (int) Math.ceil((double) records / (double) rows);
		
		if (StringUtils.isNotEmpty(searchField) && StringUtils.isNotEmpty(searchString)
				&& StringUtils.isNotEmpty(searchOper)) {
			rechercher();
		}
		
		if (StringUtils.isNotEmpty(sidx)) {
			trier();
		}
		
		paginer();
	}
	
	private void trier() {
		Collections.sort(listeTableau, new Comparator<T>() {

			@Override
			public int compare(T o1, T o2) {
				int retour = 0;
				BeanComparator<T> comparator;
				if (StringUtils.equals(sord, "desc")) {
					comparator = new BeanComparator<T>(sidx, Collections.reverseOrder());
				} else {
					comparator = new BeanComparator<T>(sidx);
				}
				retour = comparator.compare(o1, o2);
				
				return retour;
			}
		});
	}
	
	private void rechercher() {
		
	}
	
	private void paginer() {
		int from = (rows * page) - rows;
		if (from > 0) {
			this.listeTableau = listeTableau.subList(from, listeTableau.size());
		}
	}
	
	public Integer getRows() {
		return rows;
	}
	
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	public Integer getPage() {
		return page;
	}
	
	public void setPage(Integer page) {
		this.page = page;
	}
	
	public String getSord() {
		return sord;
	}
	
	public void setSord(String sord) {
		this.sord = sord;
	}
	
	public String getSidx() {
		return sidx;
	}
	
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}
	
	public String getSearchField() {
		return searchField;
	}
	
	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}
	
	public String getSearchString() {
		return searchString;
	}
	
	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}
	
	public String getSearchOper() {
		return searchOper;
	}
	
	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}
	
	public Integer getTotal() {
		return total;
	}
	
	public void setTotal(Integer total) {
		this.total = total;
	}
	
	public Integer getRecords() {
		return records;
	}
	
	public void setRecords(Integer records) {
		this.records = records;
	}

	public List<T> getListeTableau() {
		return listeTableau;
	}

	public void setListeTableau(List<T> listeTableau) {
		this.listeTableau = listeTableau;
		raffraichirTableau();
	}

}
